﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ImportOrders
{
    /// <summary>
    /// Interaction logic for dlgHistory.xaml
    /// </summary>
    public partial class dlgHistory : Window
    {
        private SalesManagerEntities _sme = new SalesManagerEntities();

        public dlgHistory()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            if (cmbOwner.SelectedValue == null)
            {
                MessageBox.Show("Please select an owner");
                return;
            }

            DateTime endDate = ((DateTime)cmbEndDate.SelectedDate).AddMinutes(1439);
            ((CollectionViewSource)FindResource("History")).Source =
                from c in _sme.tblImportBatch
                where c.OwnerID == (Int32)cmbOwner.SelectedValue
                    && c.StartDate >= cmbStartDate.SelectedDate
                    && c.StartDate <= endDate
                select c;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ((CollectionViewSource)FindResource("Owners")).Source = from c in _sme.tblOwner select c;
            cmbStartDate.SelectedDate = DateTime.Today;
            cmbEndDate.SelectedDate = DateTime.Today;
        }

        private void btnOrders_Click(object sender, RoutedEventArgs e)
        {
            tblImportBatch importBatch = (tblImportBatch)((Button)e.Source).CommandParameter;
            dlgListOrders dlg = new dlgListOrders(importBatch.OrderHeaders.ToList());
            dlg.ShowDialog();
        }

        private void btnExpand_Click(object sender, RoutedEventArgs e)
        {
            if (gridHistory.RowDetailsVisibilityMode == DataGridRowDetailsVisibilityMode.Collapsed)
            {
                ((Button)e.Source).Content = "-";
                gridHistory.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.VisibleWhenSelected;
            }
            else
            {
                ((Button)e.Source).Content = "+";
                gridHistory.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Collapsed;
            }
        }
    }
}
